/*
 * @Author: 王雁鹏
 * @Date: 2023-06-08 10:05:20
 * @LastEditTime: 2023-07-25 16:20:02
 * @LastEditors: Please set LastEditors
 * @Description: 
 * @FilePath: \expressf:\web-vue\bs-nodejs-background-code\routes\produce\service.js
 * ------------------
 */
const request = require('request');
var req = require("../../utils/router");
const db = require("../../mysql/index.js");
const config = require("./config.js");
const { generateUUID } = require("../../utils/func.js")
let { router, GetRouter, PostRouter } = req
// 设置商品
new PostRouter().send('/setProduce', function (res, data) {
    let { id, name, details, header, price, bannerImgs, status, detailsImgs, category, menu } = data
    let condition = {
        name, details, header, price, bannerImgs: JSON.stringify(bannerImgs),
        status, detailsImgs: JSON.stringify(detailsImgs), menu_id: menu, category_id: JSON.stringify(category),
        appId: id ? undefined : generateUUID()
    }
    db.setData({
        ...config,
        id,
        condition
    }, (rows, fields) => {
        res.send({ code: 200, data: rows });
    })
})
// 获取商品
new GetRouter().send('/getProduce', (res, data) => {
    let { pageSize, pageNum, name, categoryId, menu, id } = data
    let obj = {
        name: db.LIKE({ ...config, select: { name } }),
        id: db.EQ({ ...config, select: { id } }),
        category_id: db.LIKE({ ...config, select: { category_id: categoryId } }),
        menu_id: db.EQ({ ...config, select: { menu_id: menu } }),
    }
    let select = db.Params(obj)
    db.select({
        db: 'vw_produce',
        select,
        pageSize, pageNum
    }, (rows) => {
        res.send({ code: 200, data: rows });
    })
})

// 查询商品
new GetRouter().send('/selectProduce', (res, data) => {
    let { pageSize, pageNum, name, categoryId } = data
    let obj = name ? {
        name: db.LIKE({ ...config, select: { name } }),
    } : {
        category_id: db.LIKE({ ...config, select: { category_id: categoryId } }),
    }
    let select = db.Params(obj)
    db.select({
        db: 'vw_produce',
        select,
        pageSize, pageNum
    }, (rows) => {
        if (rows.list.length > 0) {
            res.send({ code: 200, data: rows });
        } else {
            let obj = {
                category_id: db.LIKE({ ...config, select: { category_id: categoryId } }),
            }
            let select = db.Params(obj)
            db.select({
                db: 'vw_produce',
                select,
                pageSize, pageNum
            }, (rows) => {
                res.send({ code: 200, data: rows });
            })
        }

    })
})
// 获取商品详情
new GetRouter().send('/details', (res, data) => {
    let { pageSize, pageNum, id } = data
    let obj = {
        id: db.EQ({ ...config, select: { id } }),
    }
    let select = db.Params(obj)
    db.select({
        db: 'vw_produce',
        select,
        pageSize, pageNum
    }, (rows) => {
        if (rows.list.length == 1) {
            res.send({ code: 200, data: rows.list[0] });
        } else {
            res.send({ code: 100, data: {} });
        }
    })
})
// 删除菜单
new GetRouter().send('/delProduce', (res, data) => {
    let { id } = data
    let obj = { id: db.EQ({ ...config, select: { id } }) }
    let select = db.Params(obj)
    db.delete({
        ...config,
        select,
    }, (rows) => {
        res.send({ code: 200, data: rows });
    })
})
module.exports = router